<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\Tools\UpdateCityList;
use App\Business\BLL\QqMap;
use App\Http\Controllers\Controller;
use App\Models\CityList;
use DB;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class CityListController extends Controller
{
    use HasResourceActions;
    private $name = '城市列表';

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header($this->name)
            ->description(trans('admin.list'))
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header($this->name)
            ->description(trans('admin.detail'))
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header($this->name)
            ->description(trans('admin.edit'))
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header($this->name)
            ->description(trans('admin.create'))
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new CityList);

        $grid->key('城市码');
        $grid->name('名称');
        $grid->lat('维度');
        $grid->lng('经度');

        $grid->disableCreateButton();
        $grid->disableRowSelector();
        $grid->disableActions();
        $grid->disableExport();

        $grid->tools(function ($tools) {
            $tools->append(new UpdateCityList());
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Citylist::findOrFail($id));

        $show->id('Id');
        $show->key('Key');
        $show->name('Name');
        $show->lat('Lat');
        $show->lng('Lng');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new CityList);

        $form->text('key', 'Key');
        $form->text('name', 'Name');
        $form->text('lat', 'Lat');
        $form->text('lng', 'Lng');

        return $form;
    }

    public function updateCityList()
    {
        //更新城市列表
        $QqMap = new QqMap();
        $list = $QqMap->getCityList();

        $datas = [[
            'key' => '000000',
            'name' => '中国',
            'lat' => '39.90469',
            'lng' => '116.40717',
            'level' => '100',
        ]];
        foreach ($list['result'] as $key => $val) {
            //排除各区
            // if ($key > 1) {
            //     continue;
            // }
            foreach ($val as $item) {
                // //排除直辖市的区
                // if ((strpos($item['id'], '11') === 0 && $item['id'] != '110000')
                //     || (strpos($item['id'], '12') === 0 && $item['id'] != '120000')
                //     || (strpos($item['id'], '31') === 0 && $item['id'] != '310000')
                //     || (strpos($item['id'], '50') === 0 && $item['id'] != '500000')) {
                //     continue;
                // }
                $data = [
                    'key' => $item['id'],
                    'name' => $item['fullname'],
                    'lat' => $item['location']['lat'],
                    'lng' => $item['location']['lng'],
                    'level' => $key,
                ];
                $datas[] = $data;
            }
        }
        DB::table('city_lists')->truncate();
        DB::table('city_lists')->insert($datas);

        $ret = [
            'status' => true,
            'message' => '更新成功',
        ];
        return response()->json($ret);

    }
}
